package com.example.synthesize.多线程.同步工具类countdownlatch;
import java.util.ArrayList;
import java.util.concurrent.CountDownLatch;

/**
 * Created by IntelliJ IDEA2022.3.2
 * @Author: Tenghw
 * @Date: 2023/03/21  13:53
 * @Description: 测试同步工具类CountDownLatch
 */
public class CountDownLatchTest {
    public static void main(String[] args) throws InterruptedException {
        final CountDownLatch latch = new CountDownLatch(3);

        ArrayList<Thread> threads = new ArrayList<>(3);
        threads.add(new Thread(new Consumer(latch,"张三")));
        threads.add(new Thread(new Consumer(latch,"李四")));
        threads.add(new Thread(new Consumer(latch,"王五")));
        threads.add(new Thread(new Waiter(latch,"服务员小芳")));
        for (Thread thread:threads){
            thread.start();
        }

        Thread.sleep(1000L);
        System.out.println("主线程开始...");
        for (Thread thread : threads) {
            thread.join();
        }
        System.out.println("所有线程都执行完毕！");
    }
}
